//
//  GWShopViewController.m
//  GiftWanted
//
//  Created by lanou on 15/11/12.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "GWShopViewController.h"
#import "GWShopModel.h"
#import "GWShopLeftCell.h"
#import "GWShopRightCell.h"
#import "GWNavigationController.h"
#import "GWShopDetailViewController.h"

@interface GWShopViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@end

@implementation GWShopViewController
- (NSMutableArray *)dataSourceArray {
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = YES;
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"menu_3"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:(GWNavigationController *)self.navigationController action:@selector(showMenu) forControlEvents:(UIControlEventTouchUpInside)];
    self.titleLabel.text = @"品牌专区";
    
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT-70)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [_mainTableView registerClass:[GWShopLeftCell class] forCellReuseIdentifier:@"leftCell"];
    [_mainTableView registerClass:[GWShopRightCell class] forCellReuseIdentifier:@"rightCell"];
    _mainTableView.rowHeight = (SCREEN_WIDTH-22)/2+65;
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_mainTableView];
    [self requestData];
}

- (void)requestData {
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.liwushuo.com/v2/brands/editor?limit=20&offset=0"]]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        [self.dataSourceArray removeAllObjects];
        for (NSDictionary *shopDic in dict[@"data"][@"brands"]) {
            GWShopModel *model = [[GWShopModel alloc] init];
            [model setValuesForKeysWithDictionary:shopDic];
            [self.dataSourceArray addObject:model];
        }
        [self.mainTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败");
    }];
    [operation start];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row%2 == 0) {
        
        GWShopLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftCell" forIndexPath:indexPath];
        GWShopModel *model = _dataSourceArray[indexPath.row];
        [cell setConfigureModel:model];
        return cell;
    } else {
        
        GWShopRightCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightCell" forIndexPath:indexPath];
        GWShopModel *model = _dataSourceArray[indexPath.row];
        [cell setConfigureModel:model];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GWShopModel *model = _dataSourceArray[indexPath.row];
    GWShopDetailViewController *detailVC = [[GWShopDetailViewController alloc] init];
    detailVC.id_s = model.id_s;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
